package com.atguigu.spzx.service.order.controller;

import com.atguigu.spzx.model.common.Result;
import com.atguigu.spzx.model.dto.h5.OrderSubmitDto;
import com.atguigu.spzx.model.entity.h5.OrderInfo;
import com.atguigu.spzx.model.vo.h5.TradeVo;
import com.atguigu.spzx.service.order.service.OrderService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * title:
 * author: zgl
 * date: 2024/1/18
 * description：
 */
@RestController
@RequestMapping("/api/order/orderInfo/auth")
public class OrderController {
    @Resource
    private OrderService orderService;

    @GetMapping("/trade")
    @Operation(summary = "确认订单接口")
    public Result trade() {
        TradeVo tradeVo = orderService.trade();
        return Result.ok(tradeVo);
    }

    @Operation(summary = "提交订单")
    @PostMapping("/submitOrder")
    public Result submitOrder(@RequestBody OrderSubmitDto submitDto) {
        return Result.ok(orderService.submitOrder(submitDto));
    }

    @Operation(summary = "根据id获取订单信息")
    @GetMapping("/{orderId}")
    public Result getOrder(@PathVariable Long orderId) {
        OrderInfo orderInfo = orderService.getById(orderId);
        return Result.ok(orderInfo);
    }

    @Operation(summary = "根据订单编号获取订单信息")
    @GetMapping("/getOrderInfoByOrderNo/{orderNo}")
    public Result<OrderInfo> getOrderInfo(@PathVariable String orderNo) {
        OrderInfo orderInfo = orderService.getOne(Wrappers.lambdaQuery(OrderInfo.class).eq(OrderInfo::getOrderNo,orderNo));
        return Result.ok(orderInfo);
    }


    @Operation(summary = "立即购买")
    @GetMapping("/buy/{skuId}")
    public Result buy(@PathVariable Long skuId) {
        TradeVo tradeVo = orderService.buy(skuId);
        return Result.ok(tradeVo);
    }


    @Operation(summary = "获取订单分页列表")
    @GetMapping("/{page}/{limit}")
    public Result getOrderList(@PathVariable Integer page, @PathVariable Integer limit, Integer orderStatus) {
        PageInfo<OrderInfo> pageInfo = orderService.getOrderList(page, limit, orderStatus);
        return Result.ok(pageInfo);
    }

    @Operation(summary = "更新订单信息")
    @PutMapping("/updateOrderStatusPayed/{orderNo}/{orderStatus}")
    public Result<OrderInfo> getOrderInfo(@PathVariable String orderNo,@PathVariable Integer orderStatus){
        orderService.updateOrderStatusPayed(orderNo,orderStatus);
        return Result.ok();
    }
}
